home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / book / applets / bounce / bouncing / bounceit.jav < prev    next >
Encoding:
Text File  |  1995-10-10  |  5.7 KB  |  247 lines

  1. /*
  2.  * %W% %E%
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.util.Hashtable;
  21. import java.applet.*;
  22. import java.io.*;
  23. import java.awt.*;
  24. import java.net.*;
  25.  
  26. /**
  27.  * @author    Jonathan Payne
  28.  * @version     %I%, %G%
  29.  */
  30.  
  31. class BounceImage {
  32.     static float inelasticity = .96f;
  33.     static float Ax = 0.0f;
  34.     static float Ay = 0.0002f;
  35.     static float Ar = 0.9f;
  36.  
  37.     public float x = 0;
  38.     public float y = 0;
  39.     public int width;
  40.     public int height;
  41.     public float Vx = 0.1f;
  42.     public float Vy = 0.05f;
  43.     public int index;
  44.     public float Vr = 0.005f + (float)Math.random() * 0.001f;
  45.     public float findex = 0f;
  46.  
  47.     BounceItem    parent;
  48.     static boolean  imagesReadIn = false;
  49.  
  50.     public void play(int n) {
  51.     if (parent.sounds[n] != null) {
  52.         parent.sounds[n].play();
  53.     }
  54.     }
  55.  
  56.     public BounceImage(BounceItem parent) {
  57.     this.parent = parent;
  58.     width = 65;
  59.     height = 72;
  60.     }
  61.  
  62.     public void move(float x1, float y1) {
  63.     x = x1;
  64.     y = y1;
  65.     }
  66.  
  67.     public void paint(Graphics g) {
  68.     int i = index;
  69.  
  70.     if (parent.bounceimages[i] == null) {
  71.         i = 0;
  72.     }
  73.     g.drawImage(parent.bounceimages[i], (int)x, (int)y, null);
  74.     }
  75.  
  76.     public void step(long deltaT) {
  77.     boolean    collision_x = false;
  78.     boolean    collision_y = false;
  79.  
  80.     float jitter = (float)Math.random() * .01f - .005f;
  81.  
  82.     x += Vx * deltaT + (Ax / 2.0) * deltaT * deltaT;
  83.     y += Vy * deltaT + (Ay / 2.0) * deltaT * deltaT;
  84.     if (x <= 0.0f) {
  85.         x = 0.0f;
  86.         Vx = -Vx * inelasticity + jitter;
  87.         collision_x = true;
  88.         play((int)(Math.random() * 3));
  89.     }
  90.     Dimension d = parent.size();
  91.     if (x + width >= d.width) {
  92.         x = d.width - width;
  93.         Vx = -Vx * inelasticity + jitter;
  94.         collision_x = true;
  95.         play((int)(Math.random() * 3));
  96.     }
  97.     if (y <= 0) {
  98.         y = 0;
  99.         Vy = -Vy * inelasticity + jitter;
  100.         collision_y = true;
  101.         play((int)(Math.random() * 3));
  102.     }
  103.     if (y + height >= d.height) {
  104.         y = d.height - height;
  105.         Vx *= inelasticity;
  106.         Vy = -Vy * inelasticity + jitter;
  107.         collision_y = true;
  108.     }
  109.     move(x, y);
  110.     Vy = Vy + Ay * deltaT;
  111.     Vx = Vx + Ax * deltaT;
  112.  
  113.     findex += Vr * deltaT;
  114.     if (collision_x || collision_y) {
  115.         Vr *= Ar;
  116.     }
  117.  
  118.     while (findex <= 0.0) {
  119.         findex += parent.bounceimages.length;
  120.     }
  121.     index = ((int) findex) % parent.bounceimages.length;
  122.  
  123.     }
  124. }
  125.  
  126. public class BounceItem extends Applet implements Runnable {
  127.     boolean images_initialized = false;
  128.     BounceImage images[];
  129.  
  130.     boolean time_to_die;
  131.     AudioClip music;
  132.     AudioClip sounds[];
  133.     Image bounceimages[];
  134.  
  135.     public BounceItem() {
  136.     }
  137.  
  138.     void makeImages(int nimages) {
  139.  
  140.     bounceimages = new Image[8];
  141.     for (int i = 1 ; i <= 8 ; i++) {
  142.         bounceimages[i-1] = getImage(getCodeBase(), "images/jon/T" + i + ".gif");
  143.         //System.out.println("d = " + bounceimages[i-1].getWidth() + "," + bounceimages[i-1].getHeight());
  144.     }
  145.  
  146.     images = new BounceImage[nimages];
  147.     for (int i = 0; i < nimages; i++) {
  148.         BounceImage    img = images[i] = new BounceImage(this);
  149.         img.move(1 + img.width * .8f * (i % 3) + i / 3 * .3f * img.width,
  150.              img.height * .3f + (i % 3) * .3f * img.height);
  151.     }
  152.  
  153.     sounds = new AudioClip[4];
  154.     sounds[0] = getAudioClip(getCodeBase(), "audio/ooh.au");
  155.     sounds[1] = getAudioClip(getCodeBase(), "audio/ah.au");
  156.     sounds[2] = getAudioClip(getCodeBase(), "audio/dah.au");
  157.     sounds[3] = getAudioClip(getCodeBase(), "audio/gong.au");
  158.     music = getAudioClip(getCodeBase(), "audio/spacemusic.au");
  159.     }
  160.  
  161.     public void run() {
  162.     long lasttime;
  163.  
  164.     try {
  165.         if (images == null) {
  166.         System.out.println("Making images ...");
  167.         makeImages(4);
  168.         }
  169.  
  170.         if (music != null) {
  171.         music.loop();
  172.         }
  173.         lasttime = System.currentTimeMillis();
  174.         while (!time_to_die) {
  175.         int i;
  176.         long now = System.currentTimeMillis();
  177.         long deltaT = now - lasttime;
  178.         boolean active = false;
  179.         Dimension d = size();
  180.  
  181.         for (i = 0; i < images.length; i++) {
  182.             BounceImage        img = images[i];
  183.  
  184.             img.step(deltaT);
  185.             if (img.Vy > .05 || -img.Vy > .05 || img.y + img.width < d.height - 10) {
  186.             active = true;
  187.             }
  188.         }
  189.         if (!active && images.length != 0) {
  190.             for (i = 0; i < images.length; i++) {
  191.             BounceImage img = images[i];
  192.  
  193.             img.Vx = (float)Math.random() / 4.0f - 0.125f;
  194.             img.Vy = -(float)Math.random() / 4.0f - 0.2f;
  195.             img.Vr = 0.05f - (float)Math.random() * 0.1f;
  196.             }
  197.             if (sounds[3] != null) {
  198.             sounds[3].play();
  199.             }
  200.         }
  201.         repaint();
  202.         lasttime = now;
  203.         try {
  204.             Thread.sleep(100);
  205.         } catch (InterruptedException e) {
  206.             return;
  207.         }
  208.         }
  209.     } finally {
  210.         if (music != null) {
  211.         music.stop();
  212.         }
  213.     }
  214.         
  215.     }
  216.  
  217.     public void init() {
  218.     Dimension d = size();
  219.     if ((d.width <= 100) || (d.height <= 100)) {
  220.         resize(500, 300);
  221.     }
  222.     }
  223.  
  224.     public void start() {
  225.     time_to_die = false;
  226.     (new Thread(this)).start();
  227.     }
  228.  
  229.     public void stop() {
  230.     time_to_die = true;
  231.     music.stop();
  232.     }
  233.  
  234.     public void paint(Graphics g) {
  235.     Dimension d = size();
  236.     g.setColor(Color.gray);
  237.     g.drawRect(0, 0, d.width - 1, d.height - 1);
  238.     if (images != null) {
  239.         for (int i = 0; i < images.length; i++) {
  240.         if (images[i] != null) {
  241.             images[i].paint(g);
  242.         }
  243.         }
  244.     }
  245.     }
  246. }
  247.